package com.douqu.game.auth.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.bean.core.util.MD5Utils;
import com.bean.core.util.TimeUtils;
import com.bean.core.util.Utils;
import com.douqu.game.auth.database.model.ClickActivationModel;
import com.douqu.game.auth.service.ClickActivationService;
import com.douqu.game.auth.service.OrderRecordService;
import com.douqu.game.core.database.model.OrderRecordModel;
import com.douqu.game.core.e.E_RechargeStatus;
import com.douqu.game.core.e.ReturnMessage;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.HttpFactory;
import com.douqu.game.core.factory.PlatformFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.web.response.BaseResponseDto;
import com.douqu.game.core.web.response.ErrorResponseDto;
import com.douqu.game.core.web.response.SuccessResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: miracle.
 * Description:
 * Date: 2018/5/16 0016 下午 3:13
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
@RestController
public class PlatformController {

    @Autowired
    private ClickActivationService clickActivationService;

    @Autowired
    OrderRecordService orderRecordService;
    /**
     * 1.当有用户点击广告位时的检测接口 由百度调用
     * @param imei_md5
     * @param os
     * @param ip
     * @param ts
     * @param pid
     * @param uid
     * @param aid
     * @param userid
     * @param click_id
     * @param callback_url
     * @param request
     * @param response
     */
    @RequestMapping(value = "/platform/baiduClick",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public void baiduClick(String imei_md5,String os,String ip,String ts,String pid,String uid,String aid,String userid,
                           String click_id,String callback_url,HttpServletRequest request, HttpServletResponse response){

        if(imei_md5 != null){
            System.out.println("imei_md5 :" + imei_md5);
        }

        if(callback_url != null){
            System.out.println("callback_url :" + callback_url);
        }



        ClickActivationModel clickActivationModel = new ClickActivationModel();
        clickActivationModel.setImei(imei_md5);
        String extInfo = callback_url.split("ext_info=")[1];
        System.out.println(extInfo);
        clickActivationModel.setExtInfo(extInfo);
        response.setStatus(clickActivationService.baiduClick(clickActivationModel));
    }

    @RequestMapping(value = "/platform/success",method = RequestMethod.GET)
    public void testSuccess(String imei,HttpServletRequest request, HttpServletResponse response){
        clickActivationService.testActivation(SGCommonProto.E_PLATFORM_TYPE.PLATFORM_TYPE_BAI_DU_VALUE,imei);
    }


    @ResponseBody
    @RequestMapping(value = "/platform/initRecharge" , method = RequestMethod.POST , produces = "application/json;charset=UTF-8")
    public BaseResponseDto initRecharge(@RequestBody OrderRecordModel orderRecordModel)
    {
        orderRecordModel.setStatus(E_RechargeStatus.NO_PAY.getCode());
        int count = orderRecordService.createOrder(orderRecordModel);

        return count == 1 ? new BaseResponseDto(ReturnMessage.SUCCESS) : new ErrorResponseDto(ReturnMessage.FAILURE);
    }



    @RequestMapping(value = HttpFactory.PLATFORM_GET_CONFIG, method = RequestMethod.POST)
    public BaseResponseDto getConfig(Integer serverId, String password, String platform)
    {
        String checkString = serverId + ConstantFactory.RECHARGE_KEY + TimeUtils.getCurrentTime(TimeUtils.YYYYMMDD);
        if(!MD5Utils.encodeUppercase(checkString).equals(password))
        {
            LogUtils.error("获取支付配置非法, 密码错误!");
            return new ErrorResponseDto(ReturnMessage.ILLEGAL);
        }

        JSONObject jsonObject = new JSONObject();
        if(Utils.isNullOrEmpty(platform))
        {
            jsonObject.put(PlatformFactory.KEY_ALIPAY, PlatformFactory.alipayConfigToJSON());
            jsonObject.put(PlatformFactory.KEY_TENPAY, PlatformFactory.tenpayConfigToJSON());
            jsonObject.put(PlatformFactory.KEY_OTHER, PlatformFactory.otherConfigToJSON());
        }
        else if(PlatformFactory.KEY_ALIPAY.equals(platform))
        {
            jsonObject.put(PlatformFactory.KEY_ALIPAY, PlatformFactory.alipayConfigToJSON());
        }
        else if(PlatformFactory.KEY_TENPAY.equals(platform))
        {
            jsonObject.put(PlatformFactory.KEY_TENPAY, PlatformFactory.tenpayConfigToJSON());
        }
        else if(PlatformFactory.KEY_OTHER.equals(platform))
        {
            jsonObject.put(PlatformFactory.KEY_OTHER, PlatformFactory.otherConfigToJSON());
        }
        return new SuccessResponseDto(jsonObject);
    }
}
